package com.pms.framework;

import java.util.*;
import org.xml.sax.*;



/**
 * <p>Title: FrontController</p>
 *
 
 * Travis
 *
 * @author 
 * @version 1.0
 */
public class ActionMappingXmlLoader extends XMLParser {

	/**
	 * The handle which will be returned for working with this object.
	 */
	private static ActionMappingXmlLoader actionMappingXmlLoader = null;
	private String pageId = null;
	private String eventId = null;
	//private boolean pageStarted = false;
	private boolean eventStarted = false;
	private boolean forwardStarted = true;
	private Hashtable actionMappingHashtable = new Hashtable();
	private Hashtable forward ;
	
	
	/**
	 * Private constructor so no one can create an instance of the class
	 */
	private ActionMappingXmlLoader() {
	}

	public static ActionMappingXmlLoader getInstance() {
		if (actionMappingXmlLoader == null) {
			actionMappingXmlLoader = new ActionMappingXmlLoader();
		}
		return actionMappingXmlLoader;
	}

	/**
	 * The method loads all the ActionElements, FormBeans and LandingPages from
	 * the XML File
	 *
	 * @param aResourceFile The resourcefile which contains all the information
	 *   of the Controller Engine in an XML format
	 */
	public Hashtable loadSettings(String aResourceFile) {
		//Load the settings from the resource file
		setXMLFile(aResourceFile);
		setHandler(this);
		try {
			processFile();
		} catch (Exception e) {
			System.out.println("\n\n>>>>>>>>>Error occured - ");
			e.printStackTrace();
		}
		return actionMappingHashtable;
	}

	/**
	 * Method to handel the StartOfElement event. This is the method where all
	 * the handling of the elements are done.
	 *
	 * @param mNamespaceURI Reprsents Name Space URI.
	 * @param mSName Name of the Element.
	 * @param mQName Represents Qualifying name of the Element.
	 * @param mAttrs Represents the Attributes in the Element.
	 */
	protected void handleStartElement(String mNamespaceURI, String mSName, String mQName,
			Attributes mAttrs) {

		//System.out.println("---------handleStartElement------------");

		if (mQName.equalsIgnoreCase("PAGE")) {
			//System.out.println("Page Element Starts...");
			ActionMap actionMapping = new ActionMap();
			if((mAttrs.getQName(0)).equalsIgnoreCase("id"))
			{
				//System.out.println("------mAttrs.getQName(0)-----------"+mAttrs.getQName(0));
				pageId = mAttrs.getValue(0);
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(0));
			}
			//pageStarted = true;
			actionMappingHashtable.put(pageId,actionMapping);
		}

		//handel Event element
		if (mQName.equalsIgnoreCase("EVENT")) {
			//System.out.println("Event Element Starts...");
			
			ActionMap actionMapping = null;
			if(eventStarted)
			{
				actionMapping = new ActionMap(); 
			}
			else
			{
				actionMapping = (ActionMap)actionMappingHashtable.get(pageId);
			}
			
			
			
			if((mAttrs.getQName(0)).equalsIgnoreCase("id"))
			{
				//System.out.println("------mAttrs.getQName(0)-----------"+mAttrs.getQName(0));
				eventId = mAttrs.getValue(0);
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(0));
			}
			if((mAttrs.getQName(1)).equalsIgnoreCase("forwardOnly"))
			{
				//System.out.println("------mAttrs.getQName(1)-----------"+mAttrs.getQName(1));
				if(mAttrs.getValue(1).equalsIgnoreCase("false"))
				{
					actionMapping.setForwardOnly(false);
				}
				else if(mAttrs.getValue(1).equalsIgnoreCase("true"))
				{
					actionMapping.setForwardOnly(true);
				}
				
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(1));
			}
			eventStarted =true;
			actionMappingHashtable.put(pageId+"_"+eventId,actionMapping);
			//System.out.println("===============page_event------"+pageId+"_"+eventId);
		}

		//handel ActionElement element
		if (mQName.equalsIgnoreCase("Validator")) {
			//System.out.println("ValidatorElement Element Starts...");
			ActionMap actionMapping = (ActionMap)actionMappingHashtable.get(pageId+"_"+eventId);
			
			if((mAttrs.getQName(0)).equalsIgnoreCase("class"))
			{
				//System.out.println("------mAttrs.getQName(0)-----------"+mAttrs.getQName(0));
				actionMapping.setValidatorClass(mAttrs.getValue(0));
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(0));
			}
			actionMappingHashtable.put(pageId+"_"+eventId,actionMapping);

		}

		//Handel Action element
		if (mQName.equalsIgnoreCase("Action")) {
			//System.out.println("Action Element Starts...");
			ActionMap actionMapping = (ActionMap)actionMappingHashtable.get(pageId+"_"+eventId);
			
			if((mAttrs.getQName(0)).equalsIgnoreCase("class"))
			{
				//System.out.println("------mAttrs.getQName(0)-----------"+mAttrs.getQName(0));
				actionMapping.setActionClass(mAttrs.getValue(0));
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(0));
			}
			actionMappingHashtable.put(pageId+"_"+eventId,actionMapping);

		}

		//Handel Landing Page Element
		if (mQName.equalsIgnoreCase("forward")) {
			//System.out.println("forward Element Starts...");
			if (forwardStarted)
			{
				forward = new Hashtable();
				forwardStarted = false;
			}
			
			
			ActionMap actionMapping = (ActionMap)actionMappingHashtable.get(pageId+"_"+eventId);
			
			
			if((mAttrs.getQName(0)).equalsIgnoreCase("name"))
			{
				//System.out.println("------mAttrs.getQName(0)-----------"+mAttrs.getValue(0));
				forward.put(mAttrs.getValue(0),mAttrs.getValue(1));
				actionMapping.setForwardList(forward);
				//System.out.println("------mAttrs.getValue(1)-----------"+mAttrs.getValue(1));
			}
			actionMappingHashtable.put(pageId+"_"+eventId,actionMapping);

		}
		

	}

	/**
	 * Method to handel the EndOfElement event.
	 *
	 * @param mNamespaceURI Reprsents Name Space URI.
	 * @param mSName Name of the Element.
	 * @param mQName Represents Qualifying name of the Element.
	 */
	protected  void handleEndElement(String mNamespaceURI, String mSName, String mQName) {
		
		
		//System.out.println("---------handleEndElement------------");

		if (mQName.equalsIgnoreCase("PAGE")) {
			//System.out.println("Page Element Ends...");
			//pageStarted = false;
			eventStarted =false;
			actionMappingHashtable.remove(pageId);
		}

		//handel Event element
		if (mQName.equalsIgnoreCase("EVENT")) {
			//System.out.println("Event Element Ends...");
			
			
			forwardStarted = true;
		}

		//handel ActionElement element
		if (mQName.equalsIgnoreCase("Validator")) {
			//System.out.println("ValidatorElement Element Ends...");

		}

		//Handel Action element
		if (mQName.equalsIgnoreCase("Action")) {
			//System.out.println("Action Element Ends...");
		}

		//Handel Landing Page Element
		if (mQName.equalsIgnoreCase("forward")) {
			//System.out.println("forward Element Ends...");
		}
		
		
		
		
	}

	protected void handleString(String mData) {
		// TODO Auto-generated method stub
		
	}

	protected void handleStartDocument() {
		// TODO Auto-generated method stub
		
	}

	protected void handleEndDocument() {
		// TODO Auto-generated method stub
		
	}

}
